//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Globalization;
using SR = Microsoft.Research.DataLayer;
using Microsoft.Research.DataLayer;

namespace Webhost
{
    /// <summary>
    /// Displays the dataproducts from the id passed through query string
    /// </summary>
    public partial class DataProductsServices : System.Web.UI.Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["dpid"] != null)
            {
                lblError.Visible = false;
                try
                {
                    ExternalRegistryHelperProxy.Initialize(Global.RegistryConnection);
                    Guid dataProductId = new Guid(Request.QueryString["dpid"].ToString(CultureInfo.InvariantCulture));
                    SR.DataProduct product = SR.DataProduct.Load(dataProductId, Global.RegistryConnection);
                    string fileName = product.Name + DataProductsServices.GetFileExtension(product.Type);

                    // This is to check if the data is retrievable or not. 
                    // If the data is not retrievable then this will throw an exception. 
                    product.Contents.GetEnumerator();

                    Response.Clear();
                    Response.ContentType = product.Type;
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
                    Response.BinaryWrite(product.Contents);
                    Response.End();
                }
                catch (Exception exp)
                {
                    lblError.InnerHtml = string.Format(CultureInfo.CurrentCulture,
                        "An error occured while retrieving data product <br /> <br /> Exception :'{0}'",
                        exp.Message);
                    lblError.Visible = true;
                }
            }
        }

        /// <summary>
        /// Gets the file extension.
        /// </summary>
        /// <param name="fileType">Type of the file.</param>
        /// <returns></returns>
        private static string GetFileExtension(string fileType)
        {
            string fileExtenstion = string.Empty;
            //// MIME type to file extension mapping
            switch (fileType)
            {
                case "image/png":
                    fileExtenstion = ".png";
                    break;
                case "image/jpg":
                    fileExtenstion = ".jpg";
                    break;
                case "image/jpeg":
                    fileExtenstion = ".jpeg";
                    break;
                case "image/gif":
                    fileExtenstion = ".gif";
                    break;
                case "image/bmp":
                    fileExtenstion = ".bmp";
                    break;
                case "text/html":
                    fileExtenstion = ".html";
                    break;
                case "text/plain":
                    fileExtenstion = ".txt";
                    break;
                case "application/ms-excel":
                    fileExtenstion = ".xlsx";
                    break;
                case "application/ms-word":
                    fileExtenstion = ".docx";
                    break;
                case "application/xml":
                    fileExtenstion = ".xml";
                    break;
                case "application/csv":
                    fileExtenstion = ".csv";
                    break;
                default:
                    fileExtenstion = string.Empty;
                    break;
            }

            return fileExtenstion;
        }
    }
}
